﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Configuration;
using System.Web;
using System.Web.Mvc;
using Ext.Net;
using Ext.Net.MVC;
using CustomMVC;
using EFMysql;
using BLL;
using System.Diagnostics;
using ShuaDanMysql.Models;

namespace ShuaDanMysql.Controllers
{
    public class SysController : BaseController
    {
        SysBll sysBll = new SysBll();
        UserBll userBll = new UserBll();

        // GET: Sys

        public ActionResult OrderTypeManagement()
        {
            return View();
        }

        public ActionResult LoadOrderType(string searchCondition, StoreRequestParameters param)
        {
            pre_shipingtype typeSearchCondition = (pre_shipingtype)JSON.Deserialize(searchCondition, typeof(pre_shipingtype));
            //string aa = param.Value;
            int pageSize = int.Parse(ConfigurationManager.AppSettings["pagesize"].ToString());
            int totalRecord = 0;
            List<pre_shipingtype> orderType = sysBll
                .GetOrderTypeList(typeSearchCondition, pageSize, param.Page, ref totalRecord).ToList();

            return this.Store(new Paging<pre_shipingtype>(orderType, totalRecord));

        }

        public ActionResult AddUser()
        {
            var groups = userBll.GetGroup();
            var admins = userBll.GetAdmin();
            ViewBag.group = groups;
            ViewBag.admin = admins;
            return View(ViewBag);
        }

        [DirectMethod]
        public ActionResult AddUserOper()
        {
            return View();
        }

        public ActionResult FunctionManagement()
        {
            return View();
        }

        public ActionResult AddFunction()
        {                        
            return View();
        }

        public ActionResult EditFunction(string id)
        {
            return View();
        }

        public ActionResult EditOrderType(string id)
        {
            return View();
        }

        public ActionResult AddOrderType()
        {
            return View();
        }

        public ActionResult AddUserGroup()
        {
            return View();
        }

        public ActionResult EditUserGroup(string id)
        {
            return View();
        }

        private pre_user GetUser()
        {
            return userBll.GetUserBySessionID(Session.SessionID);
        }

        [ErrorOper]
        [DirectMethod,OpLog("添加系统功能")]
        public ActionResult AddFunc(FunctionFront function)
        {
            Hashtable ht = new Hashtable();
            if (sysBll.CheckFuntionID(function.txtFuncID))
            {
                if (sysBll.Add(new pre_function { FUNCID = function.txtFuncID, REMARK = function.txtFuncRemark }))
                {
                    Trace.WriteLine("用户名：" + GetUser().username + "添加系统功能" + function.txtFuncID + "\n\r");
                    ht.Add("Result", "OK");
                }
                else
                {
                    ht.Add("Result", "FAIL");
                    Trace.WriteLine("用户名：" + GetUser().username + "添加系统功能失败" + function.txtFuncID + "\n\r");
                    ht.Add("Message", "添加系统功能失败");
                }
            }else
            {
                ht.Add("Result", "FAIL");
                Trace.WriteLine("用户名：" + GetUser().username + "添加系统功能失败，FUNCID已存在" + function.txtFuncID + "\n\r");
                ht.Add("Message", "添加系统功能失败");
            }
            return this.Direct(ht);

        }

        [ErrorOper]
        [DirectMethod, OpLog("修改系统功能")]
        public ActionResult ModFunc(FunctionFront function)
        {
            Hashtable ht = new Hashtable();
            if (sysBll.CheckFuntionID(function.txtFuncID))
            {
                if (sysBll.Update(new pre_function { ID = function.ID, FUNCID = function.txtFuncID, REMARK = function.txtFuncRemark }, new List<string> { "FUNCID", "REMARK" }))
                {
                    Trace.WriteLine("用户名：" + GetUser().username + "修改系统功能" + function.txtFuncID + "\n\r");
                    ht.Add("Result", "OK");
                }
                else
                {
                    ht.Add("Result", "FAIL");
                    Trace.WriteLine("用户名：" + GetUser().username + "修改系统功能失败" + function.txtFuncID + "\n\r");
                    ht.Add("Message", "修改系统功能失败");
                }
            }
            else
            {
                ht.Add("Result", "FAIL");
                Trace.WriteLine("用户名：" + GetUser().username + "修改系统功能失败，FUNCID已存在" + function.txtFuncID + "\n\r");
                ht.Add("Message", "修改系统功能失败");
            }
            return this.Direct(ht);

        }

        [ErrorOper]
        [DirectMethod, OpLog("删除系统功能")]
        public ActionResult DelFunc(FunctionFront function)
        {
            Hashtable ht = new Hashtable();
            
                if (sysBll.Remove(new pre_function{ID = function.ID}))
                {
                    Trace.WriteLine("用户名：" + GetUser().username + "删除系统功能" + function.txtFuncID + "\n\r");
                    ht.Add("Result", "OK");
                }
                else
                {
                    ht.Add("Result", "FAIL");
                    Trace.WriteLine("用户名：" + GetUser().username + "删除系统功能失败" + function.txtFuncID + "\n\r");
                    ht.Add("Message", "删除系统功能失败");
                }
          
            return this.Direct(ht);

        }

        [ErrorOper]
        [DirectMethod, OpLog("修改用户组")]
        public ActionResult ModGroup(GroupFront group)
        {
            Hashtable ht = new Hashtable();
            if (sysBll.CheckGroupTitle(group.txtGroup))
            {
                if (sysBll.Update(new pre_group { ID = group.ID.Value, TITLE = group.txtGroup }, new List<string> { "TITLE" }))
                {
                    Trace.WriteLine("用户名：" + GetUser().username + "修改用户组" + group.txtGroup + "\n\r");
                    ht.Add("Result", "OK");
                }
                else
                {
                    ht.Add("Result", "FAIL");
                    Trace.WriteLine("用户名：" + GetUser().username + "修改用户组失败" + group.txtGroup + "\n\r");
                    ht.Add("Message", "修改用户组失败");
                }
            }
            else
            {
                ht.Add("Result", "FAIL");
                Trace.WriteLine("用户名：" + GetUser().username + "修改用户组失败，TITLE已存在" + group.txtGroup + "\n\r");
                ht.Add("Message", "修改用户组失败");
            }
            return this.Direct(ht);

        }

        [ErrorOper]
        [DirectMethod, OpLog("添加用户组")]
        public ActionResult AddGroup(GroupFront group)
        {
            Hashtable ht = new Hashtable();
            if (sysBll.CheckGroupTitle(group.txtGroup))
            {
                if (sysBll.Add(new pre_group { TITLE = group.txtGroup }))
                {
                    Trace.WriteLine("用户名：" + GetUser().username + "添加用户组" + group.txtGroup + "\n\r");
                    ht.Add("Result", "OK");
                }
                else
                {
                    ht.Add("Result", "FAIL");
                    Trace.WriteLine("用户名：" + GetUser().username + "添加用户组失败" + group.txtGroup + "\n\r");
                    ht.Add("Message", "添加用户组失败");
                }
            }
            else
            {
                ht.Add("Result", "FAIL");
                Trace.WriteLine("用户名：" + GetUser().username + "添加用户组失败，TITLE已存在" + group.txtGroup + "\n\r");
                ht.Add("Message", "添加用户组失败");
            }
            return this.Direct(ht);

        }

        public ActionResult PrivilegeManagement()
        {
            return View();
        }

        public ActionResult UserGroupManagement()
        {
            return View();
        }

        public ActionResult UserManagement()
        {
            return View();
        }

        public ActionResult FeeManagement()
        {
            return View();
        }

        public ActionResult LoadUserGroup(string searchCondition, StoreRequestParameters param)
        {
            pre_group groupSearchCondition = (pre_group)JSON.Deserialize(searchCondition, typeof(pre_group));
            //string aa = param.Value;
            int pageSize = int.Parse(ConfigurationManager.AppSettings["pagesize"].ToString());
            int totalRecord = 0;
            List<pre_group> groupList = sysBll
                .GetGroupList(groupSearchCondition, pageSize, param.Page, ref totalRecord).ToList();

            return this.Store(new Paging<pre_group>(groupList, totalRecord));
        }

        public ActionResult LoadFunction(string searchCondition, StoreRequestParameters param)
        {
            pre_function functionSearchCondition = (pre_function)JSON.Deserialize(searchCondition, typeof(pre_function));
            //string aa = param.Value;
            int pageSize = int.Parse(ConfigurationManager.AppSettings["pagesize"].ToString());
            int totalRecord = 0;
            List<pre_function> functionList = sysBll
                .GetFunctionList(functionSearchCondition, pageSize, param.Page, ref totalRecord).ToList();

            return this.Store(new Paging<pre_function>(functionList, totalRecord));
        }

        public ActionResult LoadUser(string searchCondition, StoreRequestParameters param)
        {
            pre_user userSearchCondition = (pre_user)JSON.Deserialize(searchCondition, typeof(pre_user));
            //string aa = param.Value;
            int pageSize = int.Parse(ConfigurationManager.AppSettings["pagesize"].ToString());
            int totalRecord = 0;
            List<pre_user> userList = sysBll
                .GetUserList(userSearchCondition, pageSize, param.Page, ref totalRecord).ToList();

            return this.Store(new Paging<pre_user>(userList, totalRecord));
        }

        public ActionResult LoadFee(string searchCondition, StoreRequestParameters param)
        {
            pre_cost costSearchCondition = (pre_cost)JSON.Deserialize(searchCondition, typeof(pre_cost));
            //string aa = param.Value;
            int pageSize = int.Parse(ConfigurationManager.AppSettings["pagesize"].ToString());
            int totalRecord = 0;
            List<pre_cost> costList = sysBll
                .GetCostList(costSearchCondition, pageSize, param.Page, ref totalRecord).ToList();

            return this.Store(new Paging<pre_cost>(costList, totalRecord));
        }
    }
}